package ua.kpi.flats.entity;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "FLAT")
public class Flat implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "FLATID")
	private long id;

	@Max(value = 999)
	@Min(value = 1)
	@Column(name = "FLOOR")
	private int floor;
	
	@Max(value = 999)
	@Min(value = 1)
	@Column(name = "FLOORS_COUNT")
	private int floors;
	
	@DecimalMax(value = "999.99")
	@DecimalMin(value = "1")
	@Column(name = "SQUARE")
	private double square;
	
	@Max(value = 9)
	@Min(value = 1)
	@Column(name = "ROOMS")
	private int rooms;
	
	@DecimalMax(value = "999.99")
	@DecimalMin(value = "1")
	@Column(name = "LIVING_SQUARE")
	private Double livingSquare;
	
	@DecimalMax(value = "999.99")
	@DecimalMin(value = "1")
	@Column(name = "COOK_SQUARE")
	private Double cookSquare;
	
	@NotEmpty
	@Length(max = 30)
	@Column(name = "STREET")
	private String street;
	
	@NotEmpty
	@Length(max = 6)
	@Column(name = "HOUSE")
	private String house;
	
	@Length(max = 6)
	@Column(name = "FLAT")
	private String flat;
	
	@NotNull
	@DecimalMin(value = "1")
	@DecimalMax(value = "99999999.99")
	@Column(name = "PRICE")
	private BigDecimal price;
	
	@NotNull
	@ManyToOne(cascade = CascadeType.MERGE)
	@JoinColumn(name = "DISTRICTID")
	private District district;
	
	public Flat() {
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getFloor() {
		return floor;
	}

	public void setFloor(int floor) {
		this.floor = floor;
	}

	public int getFloors() {
		return floors;
	}

	public void setFloors(int floors) {
		this.floors = floors;
	}

	public double getSquare() {
		return square;
	}

	public void setSquare(double square) {
		this.square = square;
	}

	public int getRooms() {
		return rooms;
	}

	public void setRooms(int rooms) {
		this.rooms = rooms;
	}

	public Double getLivingSquare() {
		return livingSquare;
	}

	public void setLivingSquare(Double livingSquare) {
		this.livingSquare = livingSquare;
	}

	public Double getCookSquare() {
		return cookSquare;
	}

	public void setCookSquare(Double cookSquare) {
		this.cookSquare = cookSquare;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getHouse() {
		return house;
	}

	public void setHouse(String house) {
		this.house = house;
	}

	public String getFlat() {
		return flat;
	}

	public void setFlat(String flat) {
		this.flat = flat;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public District getDistrict() {
		return district;
	}

	public void setDistrict(District district) {
		this.district = district;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((cookSquare == null) ? 0 : cookSquare.hashCode());
		result = prime * result
				+ ((district == null) ? 0 : district.hashCode());
		result = prime * result + ((flat == null) ? 0 : flat.hashCode());
		result = prime * result + floor;
		result = prime * result + floors;
		result = prime * result + ((house == null) ? 0 : house.hashCode());
		result = prime * result
				+ ((livingSquare == null) ? 0 : livingSquare.hashCode());
		result = prime * result + ((price == null) ? 0 : price.hashCode());
		result = prime * result + rooms;
		long temp;
		temp = Double.doubleToLongBits(square);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((street == null) ? 0 : street.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Flat other = (Flat) obj;
		if (cookSquare == null) {
			if (other.cookSquare != null)
				return false;
		} else if (!cookSquare.equals(other.cookSquare))
			return false;
		if (district == null) {
			if (other.district != null)
				return false;
		} else if (!district.equals(other.district))
			return false;
		if (flat == null) {
			if (other.flat != null)
				return false;
		} else if (!flat.equals(other.flat))
			return false;
		if (floor != other.floor)
			return false;
		if (floors != other.floors)
			return false;
		if (house == null) {
			if (other.house != null)
				return false;
		} else if (!house.equals(other.house))
			return false;
		if (livingSquare == null) {
			if (other.livingSquare != null)
				return false;
		} else if (!livingSquare.equals(other.livingSquare))
			return false;
		if (price == null) {
			if (other.price != null)
				return false;
		} else if (!price.equals(other.price))
			return false;
		if (rooms != other.rooms)
			return false;
		if (Double.doubleToLongBits(square) != Double
				.doubleToLongBits(other.square))
			return false;
		if (street == null) {
			if (other.street != null)
				return false;
		} else if (!street.equals(other.street))
			return false;
		return true;
	}
}
